var alarmController = AlarmController();
var abnormalTableId = '#abnormalTable';
var pageNumber = null;

function AbnormalService() {
    var service = {};
    //数据的加载
    service.load = function (monitorPointId) {
        var para = service.getParameter();
        alarmController.getAlarms(monitorPointId,para.alarmDataType,'1',para.pageSize,
            para.startTime,para.endTime,
            para.alarmType,function (resp) {
            var total = resp.extra;
            if(total === 0 ){
                total = 1;
            }
            $("#pageTool").jqPaginator({
                totalPages: Math.ceil(total / para.pageSize),
                visiblePages: 1,
                currentPage: 1,
                prev: '<li><a href="javascript:void(0);" style="background-color: #060f1f"><img src="../../common/images/left.png" style="position: relative;top: 6px;"><\/a><\/li>',
                next: '<li><a href="javascript:void(0);" style="background-color: #060f1f"><img src="../../common/images/right.png" style="position: relative;top: 6px;"><\/a><\/li>',
                page: '<li><a href="javascript:void(0);" style="margin-left:5px;margin-right:5px;background-color: #060f1f;position: relative;top: 6px;"> {{page}} / {{totalPages}} <\/a><\/li>',
                onPageChange: function (pageNo,type) {
                    pageNumber = pageNo;
                    //保留第一行
                    $(abnormalTableId+" tr:not(:first)").empty("");
                    var p = service.getParameter();
                    alarmController.getAlarms(monitorPointId,p.alarmDataType,pageNo,p.pageSize,p.startTime,p.endTime,
                        p.alarmType,function (resp) {
                            // service.loadDataTable(resp.data);
                            if(resp.result){
                                service.loadDataTable(resp.data);
                            }
                            else{
                                layer.alert(resp.description);
                            }
                        });
                }
            });
        })
    };
    //获取今天的0点是23:59时间
    service.getTodayTime = function () {
        var start = new Date(new Date(new Date().toLocaleDateString()).getTime());
        var end = new Date(new Date(new Date().toLocaleDateString()).getTime()+24*60*60*1000-1);
        return {start:start,end:end};
    };
    //获取load加载所需要的所有参数
    service.getParameter = function () {
        var alarmDataType = $("#alarmDataType").val();
        console.log($("#startTime").val());
        var startTime = common.dateFormatter_inverse($("#startTime").val());
        var endTime = common.dateFormatter_inverse($("#endTime").val());
        var alarmType = $("#alarmType").val();
        var pageSize = $("#pageSize").val();
        return {
            alarmDataType:alarmDataType,
            startTime:startTime,
            endTime:endTime,
            alarmType:alarmType,
            pageSize:pageSize
        }
    };
    //将获得的数据加载到table中去
    service.loadDataTable = function (data) {
        // var d = data.reverse();
        // console.log(d);
        data.map(function (item) {
            var $trTemp = $("<tr></tr>");
            //往行里面追加 td单元格
            $trTemp.append("<td>"+ common.dateFormatter_hyphen(item.occurTime) +"</td>");
            if(item.type == 1){
                $trTemp.append("<td>"+ '超标' +"</td>");
            }else{
                $trTemp.append("<td>"+ '异常' +"</td>");
            }
            $trTemp.append("<td>"+ item.content +"</td>");
            $trTemp.appendTo(abnormalTableId);
        });
    };
    //数据的导出
    service.export = function (monitorPointId) {
        var p = service.getParameter();
        alarmController.exportAlarms(monitorPointId,p.alarmType,p.pageNo,p.pageSize,p.startTime,p.endTime,p.alarmType,function (resp) {
            console.log(resp);
        })
    };



    return service;
}

var  abnormalService = AbnormalService();

function init() {
    //时间选择器
    laydate.render({
        elem: '#startTime', //指定元素
        theme: '#060f1f',
        type: 'datetime',
        value:abnormalService.getTodayTime().start
    });
    laydate.render({
        elem: '#endTime', //指定元素
        theme: '#060f1f',
        type: 'datetime',
        value:abnormalService.getTodayTime().end
    });

    //数据的加载
    abnormalService.load(parent.monitorPointId);

    //搜索按钮
    $("#search").on('click', function () {
        $('#pageTool').jqPaginator('destroy');
        abnormalService.load(parent.monitorPointId);
    });

    //分页显示数量的重置
    $("#pageSize").on('change', function () {
        $('#pageTool').jqPaginator('destroy')
        abnormalService.load(parent.monitorPointId);
    });

    //数据导出
    $("#export").on('click', function () {
        var para = abnormalService.getParameter();
        var url=host+"/environment-protection/alarm"+"/list/export";
        var temp = url + "?monitorPointId=" + encodeURIComponent(parent.monitorPointId)+"&pageNo="+ encodeURIComponent(pageNumber);
        if (para.alarmDataType != undefined) {
            temp = temp + "&alarmDataType=" + encodeURIComponent(para.alarmDataType);
        }
        if (para.pageSize != undefined) {
            temp = temp + "&pageSize=" + encodeURIComponent(para.pageSize);
        }
        if (para.startTime != undefined) {
            temp = temp + "&startTime=" + encodeURIComponent(para.startTime);
        }
        if (para.endTime != undefined) {
            temp = temp + "&endTime=" + encodeURIComponent(para.endTime);
        }
        if (para.alarmType != undefined) {
            temp = temp + "&alarmType=" + encodeURIComponent(para.alarmType);
        }
        window.location.href = temp;
    });
}

$(function () {
    //1是污水，2是大气
    switch (parent.monitorPointId) {
        case '2':
            $("#alarmDataType").empty();
            $("#alarmDataType").append("<option selected value='4'>实时数据</option>");
            break;
        case '1':
            break;
    }
    init();
});
